﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CadastroMenu.aspx.cs" Inherits="SinacorWebTools.Web.CadastroMenu"
    EnableSessionState="True" EnableEventValidation="false" %>

<%@ Register Assembly="SinacorWebTools.Web" Namespace="SinacorWebTools.Web" TagPrefix="cc1" %>

<html>
<head id="Head1" runat="server">
    <title>Cadastramento de Menu - Sinacor+ - Ambiente DESENV</title>
    <link rel="Stylesheet" href="css/default.css" />

    <script type="text/javascript" src="js/jquery-1.5.1.min.js"></script>

    <script type="text/javascript" src="js/jquery-ui-1.8.13.custom.min.js"></script>

    <script type="text/javascript" src="js/jquery-blockui.js"></script>

    <script type="text/javascript" src="js/Funcao.js"></script>

    <script type="text/javascript" src="js/floatdiv.js"></script>

    <script type="text/javascript">
        var glbMenu = null;
        var glbLinhaSelecionada;

        function ResetForm() {
            menuForm.TxtAssembly.value = ""; ;
            menuForm.TxtClasse.value = "";
            menuForm.TxtDescricao.value = "";
            menuForm.TxtPreDescricao.value = "";
            menuForm.cboModulo.selectedIndex = 0;
            menuForm.hdnModulo.value = "";
            $("#cboMenuSuperior").html("");
            menuForm.hdnMenuSuperior.value = "";
            menuForm.cboStatus.selectedIndex = 0;
            menuForm.TxtAcao.value = "I";
            menuForm.TxtAssembly.focus();

            glbMenu = null;

            var funcao = new Object();
            funcao.CodigoFuncao = 0;
            SetFuncao(funcao);
        }

        function Validacao() {
            var TxtAssembly = menuForm.TxtAssembly;
            var TxtClasse = menuForm.TxtClasse;
            var TxtDescricao = menuForm.TxtDescricao;
            var cboModulo = menuForm.cboModulo;
            var cboMenuSuperior = menuForm.cboMenuSuperior;
            var cboStatus = menuForm.cboStatus;

            if (!TxtAssembly.value) {
                alert("Digite o Assembly do Menu.")
                TxtAssembly.focus();
                return false;
            }

            if (cboModulo.selectedIndex == 0) {
                alert("Selecione o Módulo do Menu.")
                cboModulo.focus();
                return false;
            }

            if (cboMenuSuperior.selectedIndex == 0) {
                alert("Selecione o Menu superior.")
                cboMenuSuperior.focus();
                return false;
            }

            if (cboStatus.selectedIndex == 0) {
                alert("Selecione o Status do Menu.")
                cboStatus.focus();
                return false;
            }

            if (!TxtClasse.value) {
                alert("Digite a Classe do Menu.")
                TxtClasse.focus();
                return false;
            }

            if (!TxtDescricao.value) {
                alert("Digite a Chave de Resource do Menu.")
                TxtDescricao.focus();
                return false;
            }
            return true;
        }

        function GrifaLinha(cor, linha) {
            if (glbLinhaSelecionada) {
                if (linha != glbLinhaSelecionada)
                    linha.style.backgroundColor = cor;
            }
            else
                linha.style.backgroundColor = cor;
        }

        function GridItemClick(dataGrid, linha, jsonObject) {
            var grid = eval(dataGrid);
            var linha = eval(linha);
            var rows = grid.getElementsByTagName("tr");

            for (i = 1; i <= (rows.length - 4); i++) //-4 linhas porque a customização da paginação cria tabela para renderizar a combo de páginas
                rows[i].style.backgroundColor = "#ffffff";

            glbLinhaSelecionada = linha;
            linha.style.backgroundColor = "#B2BAC6";
            glbMenu = $.parseJSON(jsonObject);

            SetForm(true);
        }

        function SetForm(assincrono) {

            if (assincrono) {
                for (i = 0; i < menuForm.cboModulo.length; i++) {
                    if (menuForm.cboModulo[i].value == glbMenu.CodigoSistema) {
                        menuForm.hdnModulo.value = glbMenu.CodigoSistema;
                        menuForm.cboModulo.selectedIndex = i;
                        break;
                    }
                }
                GetListaMenuSuperior(glbMenu.CodigoSistema);
            }
            else {
                var descricaoMenu = glbMenu.Descricao;
                var vetDescricaoMenu = descricaoMenu.split('.');

                menuForm.TxtDescricao.value = vetDescricaoMenu[vetDescricaoMenu.length - 1];
                menuForm.TxtPreDescricao.value = descricaoMenu.replace(menuForm.TxtDescricao.value, "");
                menuForm.TxtAssembly.value = glbMenu.DescricaoAssembly;
                menuForm.TxtClasse.value = glbMenu.DescricaoClasse;
                menuForm.hdnFuncaoSelecionada.Value = glbMenu.CodigoFuncao;
                menuForm.TxtCodMenu.value = glbMenu.Codigo;
                menuForm.TxtAcao.value = "A";

                SetFuncao(glbMenu.Funcao);

                for (i = 0; i < menuForm.cboMenuSuperior.length; i++) {
                    if (menuForm.cboMenuSuperior[i].value == glbMenu.CodigoMenuSuperior) {
                        menuForm.cboMenuSuperior.selectedIndex = i;
                        menuForm.hdnMenuSuperior.value = glbMenu.CodigoMenuSuperior;
                        break;
                    }
                }

                for (i = 0; i < menuForm.cboStatus.length; i++) {
                    if (menuForm.cboStatus[i].value == glbMenu.Bloqueado) {
                        menuForm.cboStatus.selectedIndex = i;
                        break;
                    }
                }
            }
        }

        function DeleteFuncao() {
            var funcao = new Object();
            funcao.CodigoFuncao = 0;
            SetFuncao(funcao);
        }

        function SetFuncao(funcao) {
            if (funcao) {
                if (funcao.CodigoFuncao > 0) {
                    menuForm.hdnFuncaoSelecionada.value = funcao.CodigoFuncao;
                    var div = document.getElementById('FuncaoSelecionada');
                    div.innerHTML = "<a href='#' onclick='PrepareShowFuncao();'>" + funcao.Sigla + " - " + funcao.Descricao + "</a>&nbsp;-&nbsp[<a href='#' onclick='DeleteFuncao();'>Remover</a>]";
                }
                else {
                    menuForm.hdnFuncaoSelecionada.value = "";
                    var div = document.getElementById('FuncaoSelecionada');
                    div.innerHTML = "<a href='#' onclick='PrepareShowFuncao();'>Selecionar Função</a>";
                }
            }
            else {
                menuForm.hdnFuncaoSelecionada.value = "";
                var div = document.getElementById('FuncaoSelecionada');
                div.innerHTML = "<a href='#' onclick='PrepareShowFuncao();'>Selecionar Função</a>";
            }
        }

        function BlockUI(elementID) {
            var prm = Sys.WebForms.PageRequestManager.getInstance();
            prm.add_beginRequest(function() {
                $("#" + elementID).block({ message: '',
                    css: {},
                    overlayCSS: { backgroundColor: '#000000', opacity: 0.4
                    }
                });
            });
            prm.add_endRequest(function() {
                $("#" + elementID).unblock();
            });
        }

        $(document).ready(function() {
            BlockUI("dvGrdMenu");
            $.blockUI.defaults.css = {};

            menuForm.TxtAcao.value = "I";
            GetListaModulo();

            $("#cboModulo").change(function() {
                GetListaMenuSuperior($(this).val());
            });
        });

        //*************** FUNÇÕES AJAX ***************

        function GetListaModulo() {
            PageMethods.GetListaModulo(ListaModuloSucesso, ListaModuloErro);
        }

        function ListaModuloSucesso(modulos) {
            var listItems = "<option value=''>Selecione...</option>";
            if (modulos.length > 0) {
                for (var i = 0; i < modulos.length; i++) {
                    listItems += "<option value='" + modulos[i].Sigla + "'>" + modulos[i].Sigla + "</option>";
                }
            }
            $("#cboModulo").html(listItems);
            $("#cboModuloPesquisa").html(listItems);
        }

        function CreateOption(text, value) {
            return option;
        }

        function ListaModuloErro(error) {
            alert("Stack Trace: " + error.get_stackTrace() + "/r/n" +
               "Error: " + error.get_message() + "/r/n" +
               "Status Code: " + error.get_statusCode() + "/r/n" +
               "Exception Type: " + error.get_exceptionType() + "/r/n" +
               "Timed Out: " + error.get_timedOut());
        }

        //*******************************************

        function GetListaMenuSuperior(siglaModulo) {
            if (siglaModulo != '0')
                PageMethods.GetListaMenuSuperior(siglaModulo, ListaMenuSuperiorSucesso, ListaMenuSuperiorErro);
            else {
                $('#lblMessage').html("");
                $("#cboMenuSuperior").html("");
                menuForm.hdnFuncaoSelecionada.value = "";
                glbMenu = null;
            }
        }

        function ListaMenuSuperiorSucesso(menus) {
            var listItems = "<option value='0'>Selecione...</option>";
            if (menus.Menu.length > 0) {
                for (var i = 0; i < menus.Menu.length; i++) {
                    listItems += "<option value='" + menus.Menu[i].Codigo + "'>" + menus.Menu[i].Descricao + "</option>";
                }
                $("#cboMenuSuperior").html(listItems);
            }

            var modulo = menuForm.cboModulo.value;

            if (menus.Modulo[modulo]) {
                menuForm.TxtPreDescricao.value = modulo + ".Menu.Label.";
                $('#lblMessage').html("");
            }
            else
                $('#lblMessage').html("Sigla do Módulo não encontrada no Web.config");

            if (glbMenu) SetForm(false);
        }

        function ListaMenuSuperiorErro() {
            alert('Erro ao recuperar a lista de menu superior');
        }

        //*******************************************        

        function PrepareShowFuncao() {
            ShowFuncao('SelecionaFuncao.aspx?modulo=' + menuForm.cboModulo.value);
        }

        //*******************************************        

        function GetParametrosMensagem(resourceKey) {
            PageMethods.GetParametrosMensagem(resourceKey, ParametrosMensagemSucesso, ParametrosMensagemErro);
        }

        function ParametrosMensagemSucesso(parametroMsg) {
            if (parametroMsg) {
                spTraducaoMenu.innerHTML = parametroMsg.Traducao;
                spUsuarioMensagem.innerHTML = parametroMsg.UsuarioCadastroMensagem;
                spUsuarioTraducao.innerHTML = parametroMsg.UsuarioCadastroTraducao;
            }
        }

        function ParametrosMensagemErro(res) {
        }

    </script>

</head>
<body style="margin-left: 8px; margin-top: 5px; margin-bottom: 0px; margin-right: 0px;">
    <form id="menuForm" runat="server">
    <asp:ScriptManager runat="server" ID="scrManager" EnablePageMethods="True" />
    <div style="font-size: 14pt; height: 55px;">
        Cadastramento de Menu <i>Sinacor+</i><br />
        <font size="4">Ambiente <font color="red"><b>DESENV</b></font></font>
    </div>
    <table style="width: 100%" border="0">
        <tr>
            <td>
                <fieldset>
                    <legend>Formulário de Cadastro</legend>
                    <table align="right" style="width: 100%; font-family: Arial, Helvetica, sans-serif;
                        font-size: 10pt" border="0">
                        <tr>
                            <td style="width: 90px;">
                                Assembly:
                            </td>
                            <td style="width: 360px;">
                                <asp:TextBox ID="TxtAssembly" runat="server" Width="357px"></asp:TextBox>
                            </td>
                            <td align="right" style="width: 50px;">
                                Status:&nbsp;
                            </td>
                            <td align="left">
                                <asp:DropDownList ID="cboStatus" runat="server" Width="357px">
                                    <asp:ListItem Value="0">Selecione...</asp:ListItem>
                                    <asp:ListItem Value="N">Ativo</asp:ListItem>
                                    <asp:ListItem Value="S">Bloqueado</asp:ListItem>
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Módulo:
                            </td>
                            <td>
                                <input type="hidden" id="hdnModulo" runat="server" />
                                <asp:DropDownList ID="cboModulo" runat="server" Width="357px" onchange="menuForm.hdnModulo.value=this.value;">
                                </asp:DropDownList>
                                <br />
                                <asp:Label runat="server" ID="lblMessage" />
                            </td>
                            <td align="right">
                                Classe:&nbsp;
                            </td>
                            <td align="left">
                                <asp:TextBox ID="TxtClasse" runat="server" Width="357px"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Menu Superior:
                            </td>
                            <td>
                                <input type="hidden" id="hdnMenuSuperior" runat="server" />
                                <asp:DropDownList ID="cboMenuSuperior" runat="server" Width="357px" onchange="menuForm.hdnMenuSuperior.value=this.value;">
                                </asp:DropDownList>
                            </td>
                            <td align="right">
                                Função:&nbsp;
                            </td>
                            <td align="left" nowrap>
                                <div id="FuncaoSelecionada">
                                    <a href="#" onclick="PrepareShowFuncao();">Selecionar Função</a>
                                </div>
                                <input type="hidden" id="hdnFuncaoSelecionada" name="hdnFuncaoSelecionada" runat="server" />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Chave Resource:
                            </td>
                            <td colspan="4">
                                <asp:UpdatePanel ID="updChaveResource" runat="server">
                                    <ContentTemplate>
                                        <asp:TextBox ID="TxtPreDescricao" runat="server" Width="280px" Enabled="False"></asp:TextBox>
                                        <asp:TextBox ID="TxtDescricao" runat="server" Width="400px"></asp:TextBox>
                                    </ContentTemplate>
                                </asp:UpdatePanel>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                &nbsp;
                            </td>
                            <td align="left" colspan="3">
                                <input type="hidden" id="TxtCodMenu" runat="server" />
                                <input type="hidden" id="TxtAcao" runat="server" />
                                &nbsp;<asp:Button ID="btnGravar" runat="server" Text="Gravar" Width="93px" OnClientClick="return Validacao();"
                                    OnClick="BtnGravar_Click" />
                            </td>
                        </tr>
                    </table>
                </fieldset>
            </td>
        </tr>
        <tr>
            <td>
                <fieldset>
                    <legend>Pesquisa</legend>
                    <table cellpadding="0" cellspacing="0" border="0" width="100%">
                        <tr valign="middle">
                            <td width="100" valign="middle">
                                Pesquisa:
                            </td>
                            <td style="width: 120px;">
                                <select id="cboModuloPesquisa" runat="server" style="width: 100px;" onchange="menuForm.hdnModuloPesquisa.value=this.value;">
                                </select>
                                <input type="hidden" id="hdnModuloPesquisa" runat="server" />
                            </td>
                            <td valign="middle" width="320">
                                <asp:TextBox ID="txtPesquisa" Text="" Width="300" runat="server" />
                            </td>
                            <td style="text-align: left;">
                                <asp:UpdatePanel ID="UpdPnlPesquisar" runat="server">
                                    <ContentTemplate>
                                        <asp:Button runat="server" ID="btnPesquisar" Text="Pesquisar" OnClick="btnPesquisar_Click"
                                            OnClientClick="ResetForm();" />
                                    </ContentTemplate>
                                </asp:UpdatePanel>
                            </td>
                        </tr>
                    </table>
                </fieldset>
            </td>
        </tr>
        <table width="100%">
            <tr valign="top">
                <td width="1">
                    <input type="button" id="btNewItem" value="Inserir novo item" onclick="ResetForm();" />
                </td>
                <td width="99%" align="right">
                    <asp:UpdatePanel ID="updPnlMensagem" runat="server">
                        <ContentTemplate>
                            <asp:Label ID="lblMensagem" runat="server" />
                        </ContentTemplate>
                    </asp:UpdatePanel>
                </td>
            </tr>
        </table>
        </tr>
        <tr>
            <td>
                <div id="dvGrdMenu">
                    <asp:UpdatePanel ID="updPnlMenu" runat="server" UpdateMode="Conditional">
                        <ContentTemplate>
                            <cc1:PagingGridView ID="grdMenu" runat="server" DataKeyNames="Codigo" OnRowDataBound="grdMenu_RowDataBound"
                                AllowPaging="True" AllowSorting="True" PageSize="10" CellPadding="2" CellSpacing="1"
                                ForeColor="#333333" Font-Size="X-Small" GridLines="None" Width="100%" AutoGenerateColumns="False"
                                OnPageIndexChanging="grdMenu_PageIndexChanging" OnSorting="grdMenu_Sorting">
                                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                <PagerStyle BackColor="#5D7B9D" ForeColor="White" HorizontalAlign="Center" Wrap="False" />
                                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                                <HeaderStyle CssClass="Link" BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"
                                    Height="26px" />
                                <EditRowStyle BackColor="#999999" />
                                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                                <Columns>
                                    <asp:BoundField HeaderText="Código" DataField="Codigo" HeaderStyle-HorizontalAlign="Center"
                                        SortExpression="Codigo">
                                        <ItemStyle Width="50px" HorizontalAlign="Center" />
                                    </asp:BoundField>
                                    <asp:TemplateField HeaderText="Chave Resource" SortExpression="Descricao" HeaderStyle-HorizontalAlign="Left">
                                        <ItemTemplate>
                                            <asp:LinkButton runat="server" ID="lnkDescricao" href="#" />
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Left" />
                                    </asp:TemplateField>
                                    <asp:BoundField DataField="Bloqueado" HeaderStyle-HorizontalAlign="Center" HeaderText="Bloqueado"
                                        SortExpression="Bloqueado">
                                        <HeaderStyle Width="70px" />
                                        <ItemStyle HorizontalAlign="Center" />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="CodigoSistema" SortExpression="CodigoSistema" HeaderStyle-HorizontalAlign="Center"
                                        HeaderText="Módulo">
                                        <HeaderStyle Width="55px" />
                                        <ItemStyle HorizontalAlign="Center" />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="SiglaFuncao" SortExpression="SiglaFuncao" HeaderText="Função"
                                        HeaderStyle-HorizontalAlign="Center">
                                        <HeaderStyle Width="60px" />
                                        <ItemStyle HorizontalAlign="Center" />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="DescricaoAssembly" SortExpression="DescricaoAssembly"
                                        HeaderText="Assembly">
                                        <HeaderStyle HorizontalAlign="Left" />
                                        <ItemStyle HorizontalAlign="Left" />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="DescricaoClasse" SortExpression="DescricaoClasse" HeaderText="Classe">
                                        <HeaderStyle HorizontalAlign="Left" />
                                        <ItemStyle HorizontalAlign="Left" />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="DescricaoMenuSuperior" SortExpression="DescricaoMenuSuperior"
                                        HeaderText="Menu Sup." Visible="False">
                                        <HeaderStyle HorizontalAlign="Left" />
                                        <ItemStyle HorizontalAlign="Left" />
                                    </asp:BoundField>
                                    <asp:TemplateField HeaderStyle-HorizontalAlign="Center">
                                        <HeaderStyle Width="50px" />
                                        <ItemStyle HorizontalAlign="Center" />
                                        <ItemTemplate>
                                            <asp:LinkButton runat="server" Text="Excluir" ID="lnkExcluir" OnClientClick="return confirm('Deseja realmente Excluir o Menu ?');"></asp:LinkButton>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                            </cc1:PagingGridView>
                        </ContentTemplate>
                        <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="btnPesquisar" EventName="Click" />
                            <asp:AsyncPostBackTrigger ControlID="BtnGravar" EventName="Click" />
                        </Triggers>
                    </asp:UpdatePanel>
                </div>
            </td>
        </tr>
    </table>
    <div id="dvTraducao" style="display: none; border: thin solid #000080; position: absolute;
        background-color: white; padding: 5px; width: 240px;">
        <table border="0" cellpadding="0" cellspacing="0" style="width: 100%">
            <tr>
                <td style="width: 183px;">
                    Tradução:
                </td>
            </tr>
            <tr>
                <td style="color: Blue;">
                    &nbsp;&nbsp;<span id="spTraducaoMenu" />
                </td>
            </tr>
            <tr>
                <td>
                    Usuário que cadastrou a mensagem:
                </td>
            </tr>
            <tr>
                <td style="color: Blue;">
                    &nbsp;&nbsp;<span id="spUsuarioMensagem" />
                </td>
            </tr>
            <tr>
                <td>
                    Usuário que cadastrou a tradução:
                </td>
            </tr>
            <tr>
                <td style="color: Blue;">
                    &nbsp;&nbsp;<span id="spUsuarioTraducao" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
